'''
Created on May 22, 2010

This Module includes the required functions to handle the configuration of
the NFS Server.
'''

import augeas

class NFS(object):
    '''
    NFS Configuration Class: Used to Add, Remove, Modify Any NFS share.
    '''

    def __init__(self):
        pass


    def getExports(self):
        '''
        getExports()
            Return a list of all exported directories.
        '''

        augeasObject = augeas.Augeas()
        directoryList = augeasObject.match('/files/etc/exports/dir')
        exportsList = []

        for directory in directoryList :
            if directory.split('/')[-1].startswith('#') :
                continue

            exportPoint = augeasObject.get(directory)
            client = augeasObject.get(directory + '/client[1]')
            options = []

            optionPointers = augeasObject.match(directory + '/client[1]/option')

            for optionPointer in optionPointers :
                options.append(augeasObject.get(optionPointer))

            exportsList.append((exportPoint, client, options))

        augeasObject.close()
        return exportsList


    def delExport(self, exportName):
        '''
        delExport(exportName)
            Delete the selected directory from the NFS exports.
        '''

        augeasObject = augeas.Augeas()
        directoryList = augeasObject.match('/files/etc/exports/dir')
        successfullyRemoved = False

        for directory in directoryList :

            if augeasObject.get(directory) == exportName :

                if augeasObject.remove(directory) :
                    successfullyRemoved = True
                    break
                else :
                    return False

        if successfullyRemoved :
            if self.save(augeasObject) :
                augeasObject.close()
                return True
            else :
                return False


    def addExport(self, exportName, client, options):
        '''
        addExport(exportName, client, options)
            Add an export to the NFS exports.
        '''

        augeasObject = augeas.Augeas()
        directoryList = augeasObject.match('/files/etc/exports/dir')
        currentIndex = len(directoryList) + 1
        currentDir = '/files/etc/exports/dir[%d]' % currentIndex

        augeasObject.set(currentDir , exportName)
        augeasObject.set(currentDir + '/client', client)

        optionIndex = 1
        for option in options :
            augeasObject.set(currentDir + '/client/option[%d]' % optionIndex , option)
            optionIndex = optionIndex + 1


        if self.save(augeasObject) :
            augeasObject.close()
            return True
        else :
            return False




    def save(self, augeasObj):
        '''
        save()
            Save the modifications to the configuration file.
        '''

        # Try to save the changes to the configuration file.
        try :
            augeasObj.save()

        except Exception :
            return False

        # Return True if everything worked.
        return True




def main():
    x = NFS()

    eList = x.getExports()
    for eName in eList :
        print eName

    if x.addExport('/home/testing', '10.0.0.1', ['ro', 'sync']) :
        print '/home/testing  was added'
    else :
        print '/home/testing  FAILED to be added'

    eList = x.getExports()
    for eName in eList :
        print eName

    if x.delExport('/home/testing') :
        print '/home/testing  was deleted'
    else :
        print '/home/testing  FAILED to be deleted'

    eList = x.getExports()
    for eName in eList :
        print eName


if __name__ == '__main__':
    main()
